package com.gaohe.controller;
import cn.hutool.crypto.digest.MD5;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaohe.common.R;
import com.gaohe.pojo.Admin;
import com.gaohe.pojo.Card;
import com.gaohe.service.CardService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Objects;

@RestController
@RequestMapping("/card")
@Slf4j
public class CardController {
    @Autowired
    CardService cardService;
//     分页查询
    @GetMapping("/page")
    public R page(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam(required = false) String name)
    {
//        Page是一个ArrayList实现，存储查询结果，PageInfo则包含更多分页信息如导航页码。
//        使用Page还是PageInfo取决于前端需求，通常PageInfo更适合提供详细的分页数据。
        Page<Card> page1 = new Page<>(page,pageSize);
        LambdaQueryWrapper<Card> qw = new LambdaQueryWrapper<>();
//        根据名字查询
        qw.like(!StringUtils.isEmpty(name),Card::getName,name);
        qw.orderByDesc(Card::getUpdateTime);
        qw.orderByAsc(Card::getName);
        return R.success(cardService.page(page1, qw));
    }

//     新增卡1
    @PostMapping
    public R save(@RequestBody Card card, HttpSession session ){
        card.setStatus(1);
        card.setCreateTime(LocalDateTime.now());
        card.setUpdateTime(LocalDateTime.now());
//     从session中获取登录信息
        Long user = (Long) session.getAttribute("user");
        card.setCreateUser(user);
        card.setUpdateUser(user);
        boolean flag = cardService.save(card);
        return flag?R.success("新增成功"):R.error("新增失败");
    }

//     编辑卡的信息
    @PutMapping
    public R update(@RequestBody Card card)
    {
        card.setUpdateTime(LocalDateTime.now());
        boolean flag = cardService.updateById(card);
        return flag?R.success("编辑成功"):R.error("编辑失败");
    }

    //    获取id
    @GetMapping("{id}")
    private R getId(@PathVariable Long id)
    {
        return R.success(cardService.getById(id));
    }

//    选择卡的状态
    @PutMapping("/changeStatus")
    public R changeStatus(@RequestBody Card card, HttpSession session)
    {
        card.setUpdateTime(LocalDateTime.now());
        card.setUpdateUser((Long) session.getAttribute("user"));
        boolean flag = cardService.updateById(card);
        return flag?R.success("修改成功"):R.error("修改失败");
    }

//    删除卡
    @DeleteMapping
    public R delete(Long id)
    {
        boolean flag = cardService.removeById(id);
        System.out.println(flag);
        if(flag)
        {
            return R.success("删除成功");
        }else{
            return R.error("删除失败");
        }
    }

//    查询所有卡
    @GetMapping("/getCardList")
    public R list(@RequestParam(required = false) String type)
    {
        LambdaQueryWrapper<Card>lwq = new LambdaQueryWrapper<>();
        lwq.eq(!StringUtils.isEmpty(type),Card::getType,type);
        lwq.eq(Card::getStatus,1);
        lwq.orderByDesc(Card::getUpdateTime);
        return R.success(cardService.list(lwq));
    }

}
